<template>
  <div class="page-container">
    <div class="blog-page">
      <!-- 内容区域容器 -->
      <div class="blog-container">
        <!-- 左侧导航栏 -->
        <div class="left-nav">
          <div class="nav-section">
            <el-button type="primary" class="new-doc-btn" @click="goToWrite">
              <i class="fa fa-plus"></i> 新增文档
            </el-button>
            <h3 class="nav-title">分类导航</h3>
            <ul class="nav-list">
              <li class="nav-item active">全部</li>
              <li class="nav-item">前端</li>
              <li class="nav-item">后端</li>
              <li class="nav-item">算法</li>
              <li class="nav-item">面试</li>
              <li class="nav-item">职场</li>
            </ul>
          </div>
          <div class="nav-section">
            <h3 class="nav-title">热门标签</h3>
            <div class="tag-list">
              <span class="tag-item">JavaScript</span>
              <span class="tag-item">Vue</span>
              <span class="tag-item">React</span>
              <span class="tag-item">Java</span>
              <span class="tag-item">Python</span>
              <span class="tag-item">算法</span>
              <span class="tag-item">面试</span>
              <span class="tag-item">职场</span>
            </div>
          </div>
        </div>

        <!-- 中间博客列表 -->
        <div class="main-content">
          <div class="blog-list">
            <div v-for="blog in blogList" :key="blog.id" class="blog-item">
              <!-- 左侧图片 -->
              <div class="blog-image">
                <img
                  v-if="blog.coverImage"
                  :src="getAssetsImge(blog.coverImage)"
                  :alt="blog.title"
                  loading="lazy"
                  @error="handleImageError(blog)"
                />
                <TextImage
                  v-else
                  :text="blog.title"
                  class="text-image-fallback"
                />
              </div>

              <!-- 右侧内容 -->
              <div class="blog-content" @click="goToDetail(blog.id)">
                <h2 class="blog-title">{{ blog.title }}</h2>
                <p class="blog-desc">{{ blog.description }}</p>

                <!-- 底部互动区 -->
                <div class="blog-footer">
                  <div class="blog-stats">
                    <span class="like">
                      <i class="fa fa-thumbs-up"></i>
                      {{ blog.likes }} 赞
                    </span>
                    <span class="dislike">
                      <i class="fa fa-thumbs-down"></i>
                      踩
                    </span>
                  </div>
                  <div class="blog-author">
                    作者: {{ blog.author }}
                  </div>
                  <div class="more-actions">
                    <i class="fa fa-ellipsis-h"></i>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 右侧推荐文章 -->
        <div class="recommend-list">
          <h3 class="recommend-title">最新推荐</h3>
          <div class="recommend-items">
            <div v-for="(article, index) in recommendList"
                 :key="article.id"
                 class="recommend-item"
                 @click="goToDetail(article.id)">
              <span class="index" :class="{ 'top-three': index < 3 }">{{ index + 1 }}</span>
              <div class="content">
                <h4 class="title">{{ article.title }}</h4>
                <div class="meta">
                  <span class="views">
                    <i class="fa fa-eye"></i>
                    {{ article.views }}
                  </span>
                  <span class="likes">
                    <i class="fa fa-thumbs-up"></i>
                    {{ article.likes }}
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getAssetsImge, DEFAULT_COVER } from '/@/utils/getImge'
import { ArticleControllerService, type ArticleQueryDTO } from '/@/api/openapi'
import TextImage from '/@/components/TextImage.vue'

const router = useRouter()

// 博客列表数据
const blogList = ref([
  {
    id: 1,
    title: '数据结构考前一天',
    description: '图：DFS、BFS、邻接矩阵，邻接表（EdgeNode,VertexNode），最小生成树（prime加点，kruskal加边），最短路径（dijkstra, floyd考试直接找路径），AOV网拓扑排序路径，AOE网（v事件，a活动）事件：最短时间（大），最长时间（倒过来小），活动：...',
    coverImage: 'Test1.jpeg',
    author: '蒟蒻的贤',
    likes: 5
  }
  // 更多博客数据...
])

onMounted(()=>{
  loadData();
})

const searchParams =ref<ArticleQueryDTO>({
  pageNum:1,
  pageSize:20
})

const loadData = async () => {
  try {
    const res = await ArticleControllerService.getArticlePage(searchParams.value)
    if (!res.data?.records) {
      blogList.value = []
      return
    }

    blogList.value = res.data.records
      .filter((item): item is NonNullable<typeof item> => item !== null && item !== undefined)
      .map(item => ({
        id: item.id ?? 0,
        title: item.title ?? '',
        description: item.summary ?? '',
        coverImage: item.thumbnail1 ?? '',
        author: item.userName ?? '',
        likes: item.likeCount ?? 0
      })) as any[]
  } catch (error) {
    console.error('加载博客列表失败:', error)
    blogList.value = []
  }
}
// 添加推荐文章列表数据
const recommendList = ref([
  {
    id: 1,
    title: '1000万行Java代码变Kotlin! Meta分享了在实现自动迁移...',
    views: 1234,
    likes: 56
  },
  {
    id: 2,
    title: '看英伟达、Meta、阿里等巨头齐聚，共同探索 C++ 在不同技术方向上...',
    views: 986,
    likes: 45
  },
  {
    id: 3,
    title: '警惕！过度抽象的代码库，是开发者的噩梦...',
    views: 876,
    likes: 34
  },
  {
    id: 4,
    title: '招了又裁？软件开发者发招聘启事...',
    views: 765,
    likes: 23
  },
  {
    id: 5,
    title: '图灵奖得主杨立昆：统治欲望源于...',
    views: 654,
    likes: 12
  }
])

// 添加发布相关的数据
const postTitle = ref('')

// 跳转到详情页
const goToDetail = (id: number) => {
  router.push(`/home/blog/article/${id}`)
}

// 添加跳转方法
const goToWrite = () => {
  router.push('/home/blog/write')
}

// 处理图片加载错误
const handleImageError = (blog: any) => {
  blog.coverImage = '' // 清空封面图片URL，触发使用TextImage组件
}
</script>

<style scoped lang="scss">
.page-container {
  display: flex;
  justify-content: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20px;

  .blog-page {
    width: 70%;
    min-width: 800px;
    max-width: 1200px;

    .blog-container {
      display: flex;
      gap: 20px;
      padding: 0;

      .left-nav {
        width: 250px;
        background: #fff;
        padding: 15px;
        border-radius: 8px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
        position: sticky;
        top: 20px;
        height: fit-content;

        .nav-section {
          margin-bottom: 20px;

          &:last-child {
            margin-bottom: 0;
          }

          .new-doc-btn {
            width: 100%;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            
            i {
              margin-right: 4px;
            }
          }

          .nav-title {
            font-size: 16px;
            font-weight: bold;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
          }

          .nav-list {
            list-style: none;
            padding: 0;
            margin: 0;

            .nav-item {
              padding: 8px 12px;
              cursor: pointer;
              border-radius: 4px;
              margin-bottom: 5px;
              transition: all 0.3s;

              &:hover {
                background: #f5f5f5;
                color: #409EFF;
              }

              &.active {
                background: #409EFF;
                color: #fff;
              }
            }
          }

          .tag-list {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;

            .tag-item {
              padding: 4px 8px;
              background: #f5f5f5;
              border-radius: 4px;
              font-size: 12px;
              cursor: pointer;
              transition: all 0.3s;

              &:hover {
                background: #409EFF;
                color: #fff;
              }
            }
          }
        }
      }

      .main-content {
        flex: 1;
        .blog-list {
          background: #fff;
          border-radius: 8px;
          box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
          padding: 20px;

          .blog-item {
            display: flex;
            padding: 12px;
            border-bottom: 1px solid #f0f0f0;
            cursor: pointer;
            transition: all 0.3s ease;
            border-radius: 8px;
            margin-bottom: 10px;
            max-height: 120px;
            overflow: hidden;

            &:hover {
              background-color: #f8f9fa;
              transform: translateY(-2px);
              box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            }

            &:last-child {
              border-bottom: none;
              margin-bottom: 0;
            }

            .blog-image {
              width: 160px;
              height: 96px;
              margin-right: 16px;
              background-color: #f5f5f5;
              border-radius: 8px;
              overflow: hidden;
              flex-shrink: 0;

              img {
                width: 100%;
                height: 100%;
                object-fit: cover;
                transition: transform 0.3s ease;

                &:hover {
                  transform: scale(1.05);
                }
              }

              .text-image-fallback {
                width: 100%;
                height: 100%;
                display: flex;
                align-items: center;
                justify-content: center;
                background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
                color: #666;
                font-size: 14px;
                font-weight: 500;
                text-align: center;
                padding: 10px;

                &:hover {
                  opacity: 0.9;
                }
              }
            }

            .blog-content {
              flex: 1;
              display: flex;
              flex-direction: column;
              min-width: 0;
              height: 100%;

              .blog-title {
                font-size: 16px;
                font-weight: 600;
                color: #333;
                margin-bottom: 8px;
                line-height: 1.4;
                white-space: nowrap;
                text-overflow: ellipsis;
                overflow: hidden;

                &:hover {
                  color: #409EFF;
                }
              }

              .blog-desc {
                color: #666;
                font-size: 13px;
                line-height: 1.5;
                margin-bottom: 8px;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: normal;
                word-break: break-all;
                flex: 1;
              }

              .blog-footer {
                display: flex;
                align-items: center;
                justify-content: space-between;
                flex-shrink: 0;
                font-size: 12px;
                color: #999;
                margin-top: auto;

                .blog-stats {
                  display: flex;
                  gap: 16px;

                  span {
                    display: flex;
                    align-items: center;
                    gap: 4px;
                    cursor: pointer;
                    transition: all 0.3s;

                    &:hover {
                      color: #409EFF;
                    }

                    i {
                      font-size: 13px;
                    }

                    &.like i {
                      color: #409EFF;
                    }

                    &.dislike i {
                      color: #999;
                    }
                  }
                }

                .blog-author {
                  color: #666;
                  font-weight: 500;
                }

                .more-actions {
                  cursor: pointer;
                  padding: 4px;
                  border-radius: 4px;
                  transition: all 0.3s;

                  &:hover {
                    background: #f5f5f5;
                    color: #409EFF;
                  }

                  i {
                    font-size: 14px;
                  }
                }
              }
            }
          }
        }
      }

      .recommend-list {
        width: 300px;
        background: #fff;
        padding: 15px;
        border-radius: 8px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
        position: sticky;
        top: 20px;
        height: fit-content;

        .recommend-title {
          font-size: 18px;
          font-weight: bold;
          margin-bottom: 15px;
          padding-bottom: 10px;
          border-bottom: 1px solid #eee;
        }

        .recommend-items {
          .recommend-item {
            display: flex;
            align-items: flex-start;
            padding: 12px 0;
            cursor: pointer;
            border-bottom: 1px solid #f5f5f5;

            &:hover {
              .title {
                color: #409EFF;
              }
            }

            .index {
              width: 24px;
              height: 24px;
              line-height: 24px;
              text-align: center;
              background: #f5f5f5;
              border-radius: 4px;
              margin-right: 12px;
              font-size: 14px;
              color: #666;

              &.top-three {
                background: #409EFF;
                color: #fff;
              }
            }

            .content {
              flex: 1;

              .title {
                font-size: 14px;
                line-height: 1.4;
                margin-bottom: 8px;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;
                overflow: hidden;
              }

              .meta {
                display: flex;
                gap: 15px;
                color: #999;
                font-size: 12px;

                span {
                  display: flex;
                  align-items: center;
                  gap: 4px;

                  i {
                    font-size: 12px;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>

